package com.tianqu.tms.www.car.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.util.car.CarInformationApi;
import com.tianqu.tms.core.util.car.IdentifyModelByVinBean;
import com.tianqu.tms.dao.MemberDao;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dto.MemberDto;
import com.tianqu.tms.dto.TmsCarmodelVinDto;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.CarModelInfo;
import com.tianqu.tms.entity.CarStyle;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.CommercialVehicle;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.VehicleBrand;
import com.tianqu.tms.www.car.actionform.CarStockModifyForm;
import com.tianqu.tms.www.car.controller.CarStockModifyResultController;
import com.tianqu.tms.www.car.viewhelper.CarStockModifyViewhelper;

public class CarStockModifyResultAction extends TmsAction{
	private static final String LOADIMG = "imgLoad";//跳转到图片上传
	private static final String IMGKEY = "imgKey";// 图片位置
	private static final String IMGTYPE = "imgType";// 图片类型
	private static final String READCARNAME = "readCarName";// 读取车辆名称

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response, UserContext uc) throws Exception {
		CarStockModifyForm inputForm = (CarStockModifyForm)form; 
		String forward = "";
		
		//
		CarStockModifyViewhelper viewhelper = new CarStockModifyViewhelper();
		viewhelper = (CarStockModifyViewhelper)uc.getAttribute(VIEWHELP);

		//
		String styleName = "";
		if(!StringUtil.isNullOrBlank(inputForm.getCarStyleId())){
			CarStyle carStyle = CarStyle.getInstance();
			String bb = "";
			if(inputForm.getCarStyleId().startsWith("S")){
				CommercialVehicle commercialVehicle = CommercialVehicle.getInstance();
				styleName = commercialVehicle.getCarStyleRNameById(inputForm.getCarStyleId());
				String dis[] = styleName.split("-");  bb = dis[1];
			}else{
				styleName = carStyle.getCarStyleRNameById(inputForm.getCarStyleId());
				bb = styleName;
			}
			viewhelper.setDisplacementS(bb);
		}
		viewhelper.setIsShelve(inputForm.getIsShelve());
		viewhelper.setSerial(inputForm.getSerial());
		viewhelper.setColor(String.valueOf(inputForm.getColor()));
		viewhelper.setAttestStatus(inputForm.getAttestStatus());
		viewhelper.setNote(inputForm.getNote());
		viewhelper.setLicensePlate(inputForm.getLicensePlate());
		viewhelper.setVendorId(inputForm.getVendorId());
		viewhelper.setBrandId(inputForm.getBrandId());
		viewhelper.setCarStyleId(inputForm.getCarStyleId());
		viewhelper.setCarStyle(inputForm.getCarStyleId());
		viewhelper.setRegDate(inputForm.getRegDate());
		viewhelper.setCarScore(inputForm.getCarScore());
		viewhelper.setPrice(String.valueOf(inputForm.getPrice()));
		viewhelper.setMileage(String.valueOf(inputForm.getMileage()));		
		viewhelper.setMemberId(String.valueOf(inputForm.getMemberId()));
		viewhelper.setCarOwner(inputForm.getCarOwner());
		viewhelper.setCardType(inputForm.getCardType());
		viewhelper.setBackFlag(0);
		viewhelper.setJqx(inputForm.getJqx());
		Member member = Member.getInstance();
		Codes code = Codes.getInstance();
		viewhelper.setMemberList(member.findAllMemInfo());// 会员类型
		viewhelper.setCardTypeList(code.getCodeslistByItemId(Codes.PLATE_TYPE));// 号牌种类list
		viewhelper.setChestr(inputForm.getChestr());
		viewhelper.setMemId(inputForm.getMemId());
		viewhelper.setMemType(inputForm.getMemType());
		viewhelper.setSmjf(inputForm.getSmjf());
		viewhelper.setYearCarefulPeriod(inputForm.getYearCarefulPeriod());
		viewhelper.setCompanyContactPerson(inputForm.getCompanyContactPerson());
		viewhelper.setCompanyContactPhone(inputForm.getCompanyContactPhone());
		viewhelper.setVin(inputForm.getVin());
		viewhelper.setTempLicensePlate(inputForm.getTempLicensePlate());
		viewhelper.setRemark(inputForm.getRemark());
		viewhelper.setPhoto_1(inputForm.getImg1());
		viewhelper.setPhoto_2(inputForm.getImg2());
		viewhelper.setPhoto_3(inputForm.getImg3());
		viewhelper.setPhoto_4(inputForm.getImg4());
		viewhelper.setCarName(styleName);
		viewhelper.setPhoto_5(inputForm.getImg5());
		viewhelper.setPhoto_6(inputForm.getImg6());
		viewhelper.setPhoto_7(inputForm.getImg7());
		viewhelper.setPhoto_8(inputForm.getImg8());
		viewhelper.setLdpz1(inputForm.getLdpz1());
		viewhelper.setLdpz2(inputForm.getLdpz2());
		viewhelper.setLdpz3(inputForm.getLdpz3());
		viewhelper.setLdpz4(inputForm.getLdpz4());
		viewhelper.setLdpz5(inputForm.getLdpz5());
		viewhelper.setLdpz6(inputForm.getLdpz6());
		viewhelper.setLdpz7(inputForm.getLdpz7());
		viewhelper.setLdpz8(inputForm.getLdpz8());
		viewhelper.setLdpz9(inputForm.getLdpz9());
		viewhelper.setLdpz10(inputForm.getLdpz10());
		viewhelper.setLdpz11(inputForm.getLdpz11());
		viewhelper.setLdpz12(inputForm.getLdpz12());
		viewhelper.setLdpz13(inputForm.getLdpz13());
		viewhelper.setLdpz14(inputForm.getLdpz14());
		viewhelper.setLdpz15(inputForm.getLdpz15());
		viewhelper.setLdpz16(inputForm.getLdpz16());
		viewhelper.setLdpz17(inputForm.getLdpz17());
		viewhelper.setLdpz18(inputForm.getLdpz18());
		viewhelper.setLdpz19(inputForm.getLdpz19());
		viewhelper.setLdpz20(inputForm.getLdpz20());
		viewhelper.setLdpz21(inputForm.getLdpz21());
		viewhelper.setLdpz22(inputForm.getLdpz22());
		viewhelper.setLdpz23(inputForm.getLdpz23());
		viewhelper.setLdpz24(inputForm.getLdpz24());
		viewhelper.setLdpz25(inputForm.getLdpz25());
		viewhelper.setLdpz26(inputForm.getLdpz26());
		viewhelper.setLdpz27(inputForm.getLdpz27());
		viewhelper.setLdpz28(inputForm.getLdpz28());
		viewhelper.setLdpz29(inputForm.getLdpz29());
		viewhelper.setLdpz30(inputForm.getLdpz30());
		viewhelper.setLdpz31(inputForm.getLdpz31());
		viewhelper.setLdpz32(inputForm.getLdpz32());
		viewhelper.setLdpz33(inputForm.getLdpz33());
		viewhelper.setLdpz34(inputForm.getLdpz34());
		viewhelper.setLdpz35(inputForm.getLdpz35());
		viewhelper.setLdpz36(inputForm.getLdpz36());
		viewhelper.setLdpz37(inputForm.getLdpz37());
		viewhelper.setLdpz38(inputForm.getLdpz38());
		viewhelper.setLdpz39(inputForm.getLdpz39());
		viewhelper.setLdpz40(inputForm.getLdpz40());

		//车辆名称取值
		VehicleBrand brand = VehicleBrand.getInstance();
		String brandIdStr = "";
		if(StringUtil.isNullOrBlank(inputForm.getCarBrand())){
			brandIdStr = inputForm.getBrandId();
		}else{
			brandIdStr = inputForm.getCarBrand();
		}
		CommercialVehicle commercialVehicle = CommercialVehicle.getInstance();
		if (StringUtil.isNullOrBlank(inputForm.getCarBrand()) && StringUtil.isNullOrBlank(inputForm.getBrandId())) {
			viewhelper.setCarName(" ");
//			viewhelper.setGearbox("");
			viewhelper.setCountryStyle("");
		} else {
			List list = new ArrayList();
			if(inputForm.getFromType() == 1){
				list = brand.getAllVehicleBrandInfo(inputForm.getVendorId());
			}else{
				list = commercialVehicle.getAllVehicleBrandInfo(inputForm.getVendorId());
			}
			
			if (list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					Map map = (Map) list.get(i);
					if (map.get("brandId").equals(brandIdStr)) {
						String carName = (String) map.get("brandName");
						viewhelper.setCarName(carName);
					}
				}
			}else{
				viewhelper.setCarName(" ");
			}
		}
		
		viewhelper.setIsMatch(inputForm.getIsMatch());
		if(inputForm.getIsMatch() == 2)
		{
			viewhelper.setCarName(inputForm.getCarName());
		}
		
		//
		if(inputForm.getImgUpLoad1()!=null)
		{
			//往viewhelp中放一个值
			uc.setAttribute(IMGTYPE, "1");
			uc.setAttribute(IMGKEY, "1");
			forward = LOADIMG;
		}else  if(inputForm.getImgUpLoad2()!=null)
		{
			//往viewhelp中放一个值
			uc.setAttribute(IMGTYPE, "1");
			uc.setAttribute(IMGKEY, "2");
			forward = LOADIMG;
		}else  if(inputForm.getImgUpLoad3()!=null)
		{
			//往viewhelp中放一个值
			uc.setAttribute(IMGTYPE, "1");
			uc.setAttribute(IMGKEY, "3");
			forward = LOADIMG;
		}else  if(inputForm.getImgUpLoad4()!=null)
		{
			//往viewhelp中放一个值
			uc.setAttribute(IMGTYPE, "1");
			uc.setAttribute(IMGKEY, "4");
			forward = LOADIMG;
		}else  if(inputForm.getImgUpLoad5()!=null)
		{
			//往viewhelp中放一个值
			uc.setAttribute(IMGTYPE, "1");
			uc.setAttribute(IMGKEY, "5");
			forward = LOADIMG;
		}else  if(inputForm.getImgUpLoad6()!=null)
		{
			//往viewhelp中放一个值
			uc.setAttribute(IMGTYPE, "1");
			uc.setAttribute(IMGKEY, "6");
			forward = LOADIMG;
		}else  if(inputForm.getImgUpLoad7()!=null)
		{
			//往viewhelp中放一个值
			uc.setAttribute(IMGTYPE, "1");
			uc.setAttribute(IMGKEY, "7");
			forward = LOADIMG;
		}else  if(inputForm.getImgUpLoad8()!=null) {
			//往viewhelp中放一个值
			uc.setAttribute(IMGTYPE, "1");
			uc.setAttribute(IMGKEY, "8");
			forward = LOADIMG;
		}else if(inputForm.getReadCarNameBtn()!= null)
		{
			CarModelInfo modelInfo = CarModelInfo.getInstance();
			TmsCarmodelVinDto carmodelVin = modelInfo.carmodelVinGet(null,viewhelper.getVin());
			if(carmodelVin == null || StringUtil.isNullOrBlank(carmodelVin.getModelId())){
				if(inputForm.getVinNum().equals(viewhelper.getVin())){
					if (inputForm.getIsMatch() == 2)
					{
						setErrorsToRequest(request, TmsConstants.U8015, null); 
						request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
					}else {				
						IdentifyModelByVinBean identifyModelByVinBean = CarInformationApi.getIdentifyModelByVin(inputForm.getVin());
						if (!StringUtil.isNullOrBlank(identifyModelByVinBean.getModelId()))
						{							
							CarStyle carStyle = CarStyle.getInstance();
							carStyle = carStyle.findCarStyleInfo(identifyModelByVinBean.getModelId());
							if(!StringUtil.isNullOrBlank(carStyle.getCarStyleId())){
								viewhelper.setIsMatch(2);
								viewhelper.setCarName(carStyle.getCarStyleName());
								viewhelper.setCarStyleId(carStyle.getCarStyleId());
								viewhelper.setBrandId(carStyle.getBrandId());
								viewhelper.setVendorId(carStyle.getVendorId());
								setErrorsToRequest(request, TmsConstants.U8015, null); 
								request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
							}else{
								setErrorsToRequest(request, TmsConstants.U8016, null); 
								request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
							}
							
						}else{			
							setErrorsToRequest(request, TmsConstants.U8016, null); 
							request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
						}
					}
				}else {
					IdentifyModelByVinBean identifyModelByVinBean = CarInformationApi.getIdentifyModelByVin(inputForm.getVin());
					if (!StringUtil.isNullOrBlank(identifyModelByVinBean.getModelId()))
					{
						CarStyle carStyle = CarStyle.getInstance();
						carStyle = carStyle.findCarStyleInfo(identifyModelByVinBean.getModelId());
						if(!StringUtil.isNullOrBlank(carStyle.getCarStyleId())){
							viewhelper.setIsMatch(2);
							viewhelper.setCarName(carStyle.getCarStyleName());
							viewhelper.setCarStyleId(carStyle.getCarStyleId());
							viewhelper.setBrandId(carStyle.getBrandId());
							viewhelper.setVendorId(carStyle.getVendorId());
							setErrorsToRequest(request, TmsConstants.U8015, null); 
							request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
						}else{
							setErrorsToRequest(request, TmsConstants.U8016, null); 
							request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
						}
					}else{
						setErrorsToRequest(request, TmsConstants.U8016, null); 
						request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
					}
				}
			}else{
				if(inputForm.getVinNum().equals(viewhelper.getVin())){
					if (inputForm.getIsMatch() == 2){					
						setErrorsToRequest(request, TmsConstants.U8015, null); 
						request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
					}else {				
							viewhelper.setIsMatch(2);
							
							CarStyle carStyle = CarStyle.getInstance();
							VehicleBrand vehicleBrand = VehicleBrand.getInstance();
							String styleNmae = carStyle.getCarStyleRNameById(carmodelVin.getModelId());
							String brandId = carStyle.getBrandIDByCarStyleID(carmodelVin.getModelId());
							String vendorId = vehicleBrand.getVendorIdByBrandId(brandId);
							viewhelper.setCarName(styleNmae);
							viewhelper.setCarStyleId(carmodelVin.getModelId());
							viewhelper.setBrandId(brandId);
							viewhelper.setVendorId(vendorId);							
							setErrorsToRequest(request, TmsConstants.U8015, null); 
							request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
					}
				}else {
					viewhelper.setIsMatch(2);
					
					CarStyle carStyle = CarStyle.getInstance();
					VehicleBrand vehicleBrand = VehicleBrand.getInstance();
					String styleNmae = carStyle.getCarStyleRNameById(carmodelVin.getModelId());
					String brandId = carStyle.getBrandIDByCarStyleID(carmodelVin.getModelId());
					String vendorId = vehicleBrand.getVendorIdByBrandId(brandId);
					viewhelper.setCarName(styleNmae);
					viewhelper.setCarStyleId(carmodelVin.getModelId());
					viewhelper.setBrandId(brandId);
					viewhelper.setVendorId(vendorId);							
					setErrorsToRequest(request, TmsConstants.U8015, null); 
					request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
				}
			}
			forward = READCARNAME;
		}
		else{//save		
			if(!inputForm.getVinNum().equals(viewhelper.getVin())){
				Car carInfo = Car.getInstance();
				// 使用车架号（VIN）+ 车辆库存状态（registerStatus）不为“4：存档”
				String memberId = carInfo.vinCheckNotKeep(inputForm.getVin());
				if (!StringUtil.isNullOrBlank(memberId)){
					MemberDao mDao = (MemberDao) TmsDao.getInstance(TmsDao.TMS_MEMBER);
					MemberDto memberDto = mDao.findMemberInfoByMemId(memberId);
					String[] param = {memberDto.getShortName()};
					setErrorsToRequest(request, TmsConstants.U8007, param); // 输入的车架号已经被【{0}】登记使用，不能重复登记。
					request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
					return mapping.getInputForward();
				}
			}
			CarStockModifyResultController controller = new CarStockModifyResultController();
			controller.execute(uc);
			
			viewhelper = new CarStockModifyViewhelper();
			request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
			forward = TmsConstants.SUCCESS;
		}
		
		return mapping.findForward(forward);
	}

}
